package com.crowd.web.controller;

import com.crowd.entity.Role;
import com.crowd.service.api.IRoleService;
import com.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Stone
 * @date 2020/5/18 14:59
 */
@Controller
public class RoleController {
    @Autowired
    private IRoleService roleService;

    Logger logger = LoggerFactory.getLogger(RoleController.class);

    /**
     * @Author:Stone
       * @Param pageNum:
     * @Param pageSize:
     * @Param keyword:
       * @return: com.crowd.util.ResultEntity<com.github.pagehelper.PageInfo<com.crowd.entity.Role>>
     * @Date: 2020/5/19 15:46
     * @Description:
    */
    @PreAuthorize("hasRole('角色菜单管理员')")
    @RequestMapping("/role/get/page/info")
    @ResponseBody
    public ResultEntity<PageInfo<Role>> getPaeInfo(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "keyword",defaultValue = "") String keyword){
        try {
            PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);
            return ResultEntity.successWithData(pageInfo);
        }catch (Exception e){
            return ResultEntity.failed(e.getMessage());
        }
    }

    /**
     * @Author:Stone
       * @Param role:
       * @return: com.crowd.util.ResultEntity
     * @Date: 2020/5/19 15:50
     * @Description:
    */
    @RequestMapping("/role/save")
    @ResponseBody
    public ResultEntity<String> saveRole(Role role){
        try {
            roleService.saveRole(role);
            return ResultEntity.successWithoutData();
        }catch (Exception e){
            return ResultEntity.failed(e.getMessage());
        }

    }

    /**
     * @Author:Stone
     * @Param role:
     * @return: com.crowd.util.ResultEntity
     * @Date: 2020/5/19 15:50
     * @Description:
     */
    @RequestMapping("/role/update")
    @ResponseBody
    public ResultEntity<String> updateRole(Role role){
        try {
            logger.info(role.toString());
            roleService.updateRole(role);
            return ResultEntity.successWithoutData();
        }catch (Exception e){
            return ResultEntity.failed(e.getMessage());
        }

    }

    /**
     * @Author:Stone
       * @Param roleIdList:
       * @return: com.crowd.util.ResultEntity<java.lang.String>
     * @Date: 2020/5/20 14:39
     * @Description:删除role，此处可做单条删除，也可做批量删除
    */
    @ResponseBody
    @RequestMapping("/role/remove")
    public ResultEntity<String> removeRole(@RequestBody List<Integer> roleIdList){
        try {
            roleService.removeRole(roleIdList);
            return ResultEntity.successWithoutData();
        }catch (Exception e){
            return ResultEntity.failed(e.getMessage());
        }
    }
}
